package ytmc.setup;

import ytmc.setup.security.*;
import java.util.Observable;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.XMLConfiguration;
import ytmc.Model;


/**
 *
 * @author Kicok
 */
public class SetupModel extends Observable implements Model
{
    private XMLConfiguration config;
    private PasswordCipher cipher;

    private String login;
    private String password;

    private VideoFormat videoFormat;
    private VideoResolution videoResolution;
    private int videoFrameRate;

    private AudioFormat audioFormat;
    private AudioQuality audioQuality;


    public SetupModel()
    {
        this.config = Config.load();
        this.cipher = new AESPasswordCipher();

        loadSetup();
    }

    public final void loadSetup()
    {
        try
        {
            setLogin( config.getString( "login" ) );
            setPassword( cipher.decrypt( config.getString( "password" ) ) );

            setVideoFormat( ConfigUtils.getEnum( config, "video-format", VideoFormat.DEFAULT_FORMAT ) );
            setVideoResolution( ConfigUtils.getEnum( config, "video-resolution", VideoResolution.DEFAULT_RESOLUTION ) );
            setVideoFrameRate( getValidVideoFrameRate( config.getInt( "video-frame-rate", 0 ) ) );

            setAudioFormat( ConfigUtils.getEnum( config, "audio-format", AudioFormat.DEFAULT_FORMAT ) );
            setAudioQuality( ConfigUtils.getEnum( config, "audio-quality", AudioQuality.DEFAULT_QUALITY ) );

            config.save();

            setChanged();
            notifyObservers( "form-changed" );
        }
        catch( ConfigurationException e )
        {
            System.out.println( "Nie udało się wczytać konfiguracji" );
            System.out.println( e );
        }
    }

    private int getValidVideoFrameRate( int frameRate )
    {
        return Math.max( 0, Math.min( 60, frameRate ) );
    }


    public void saveSetup()
    {
        try
        {
            config.setProperty( "login", getLogin() );
            config.setProperty( "password", cipher.encrypt( getPassword() ) );

            config.setProperty( "video-format", getVideoFormat().toString() );
            config.setProperty( "video-resolution", getVideoResolution().toString() );
            config.setProperty( "video-frame-rate", getVideoFrameRate() );

            config.setProperty( "audio-format", getAudioFormat().toString() );
            config.setProperty( "audio-quality", getAudioQuality().toString() );

            config.save();
        }
        catch( ConfigurationException e )
        {
            System.out.println( "Nie udało się zapisać konfiguracji" );
            System.out.println( e );
        }
    }


    public String getLogin()
    {
        return login;
    }

    public void setLogin( String login )
    {
        this.login = login;
    }

    public String getPassword()
    {
        return password;
    }

    public void setPassword( String password )
    {
        this.password = password;
    }

    public VideoFormat getVideoFormat()
    {
        return videoFormat;
    }

    public void setVideoFormat( int i )
    {
        setVideoFormat( VideoFormat.get( i ) );
    }

    public void setVideoFormat( VideoFormat videoFormat )
    {
        this.videoFormat = videoFormat;
    }

    public VideoResolution getVideoResolution()
    {
        return videoResolution;
    }

    public void setVideoResolution( int i )
    {
        setVideoResolution( VideoResolution.get( i ) );
    }

    public void setVideoResolution( VideoResolution videoResolution )
    {
        this.videoResolution = videoResolution;
    }

    public int getVideoFrameRate()
    {
        return videoFrameRate;
    }

    public void setVideoFrameRate( int videoFrameRate )
    {
        this.videoFrameRate = videoFrameRate;
    }

    public AudioFormat getAudioFormat()
    {
        return audioFormat;
    }

    public void setAudioFormat( int i )
    {
        setAudioFormat( AudioFormat.get( i ) );
    }

    public void setAudioFormat( AudioFormat audioFormat )
    {
        this.audioFormat = audioFormat;
    }

    public AudioQuality getAudioQuality()
    {
        return audioQuality;
    }

    public void setAudioQuality( int i )
    {
        setAudioQuality( AudioQuality.get(  i ) );
    }

    public void setAudioQuality( AudioQuality audioQuality )
    {
        this.audioQuality = audioQuality;
    }
}